let valData = null
$(function () {
    valData = JSON.parse(sessionStorage.getItem("data"));
    //console.log(valData.username)
    setUserData(valData)
});

function setUserData(data) {
    layui.use(['form'], function () {
        let form = layui.form;
        $("#L_username").val(data.username);
        $("#L_phone").val(data.phone);
        $("#L_birthday").val(data.birthday);
        $("#L_email").val(data.email);
        $("#L_address").val(data.address);
        $("#L_pass").val(data.password);
        $("input[name=sex][value=" + data.sex + "]").attr("checked", true);
        let hobby = data.hobby.split(",");
        for (let j = 0; j < hobby.length; j++) {
            $("input[name=hobby]").each(function () {
                if (hobby[j] == $(this).val()) {
                    $(this).prop("checked", true);
                }
            })
        }
        $("input[name=enable][value=" + data.enable + "]").attr("selected", true);
        $("#L_remark").val(data.remark);
        form.render();
    });
}
    layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        let laydate = layui.laydate;
        $ = layui.jquery;
        let form = layui.form,
            layer = layui.layer;
        //执行一个laydate实例
        laydate.render({
            elem: '#L_birthday' //指定元素
        });
        //自定义验证规则
        form.verify({
            username: [/^[\w-]{4,16}$/, "你输入的格式不正确"],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(edit)',
            function (data) {
                data = data.field;
                data.hobby = getHobby().toLocaleString();
                data.id = valData.id
                //console.log(JSON.stringify(data));
                $.ajax({
                    url: 'http://localhost:8081/back/user/update',//地址：访问后台的地址
                    data: {val:JSON.stringify(data)},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        console.log(data);
                        if (data.code === 0) {
                            //发异步，把数据提交给php
                            layer.alert("编辑成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert("编辑失败", {
                                icon: 1
                            });
                        }
                    }

                });
                return false;
            });

    });

//获取提现门槛复选框的值
function getHobby() {
    var value = [];
    $('input[name=hobby]:checked').each(function () {
        //这里用所选复选框或运算后的值
        value.push($(this).val());
    });
    return value;
}